package com.ruoyi.common.aliyun;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import com.ruoyi.common.utils.ProjectConstant;
import com.aliyuncs.dypnsapi.model.v20170525.*;
/**
 * 	根据token获取手机号码
 * @author DEREK
 *
 */
public class GetMobile {

	
	/**
	 * 	根据token获取手机号码
	 * @param token
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public static String getMobileByToken(String token) {
    	DefaultProfile profile = DefaultProfile.getProfile("cn-shenzhen", ProjectConstant.ACCESSKEY_ID, ProjectConstant.ACCESSKEY_SECRET);
        IAcsClient client = new DefaultAcsClient(profile);

        GetMobileRequest request = new GetMobileRequest();
        request.setRegionId("cn-shenzhen");
        request.setAccessToken(token);
        try {
            GetMobileResponse response = client.getAcsResponse(request);
            String json = new Gson().toJson(response);
            System.out.println(json);
            JSONObject jsonO = JSONObject.parseObject(json);
            if("OK".equals(jsonO.getString("code"))) {
            	 JSONObject jsonMobile = (JSONObject)jsonO.get("getMobileResultDTO");
            	 return jsonMobile.getString("mobile");
            }
           return "";
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }
}
